<template>
  <div class="user-center">
    <div class="container">
      <div class="user-center-layout">
        <!-- 左侧导航栏 -->
        <div class="sidebar">
          <div class="user-brief">
            <el-avatar :size="80" :src="userInfo.icon" />
            <h3>{{ userInfo.nickName }}</h3>
            <p>{{ userInfo.email }}</p>
          </div>
          <div class="nav-menu">
            <router-link
              v-for="item in menuItems" 
              :key="item.key"
              :to="{ name: item.routeName }"
              custom
              v-slot="{ navigate, isActive }"
            >
              <div
                class="menu-item"
                :class="{ active: isActive }"
                @click="navigate"
              >
                <el-icon><component :is="item.icon" /></el-icon>
                <span>{{ item.label }}</span>
              </div>
            </router-link>
          </div>
        </div>

        <!-- 右侧内容区 -->
        <div class="content-area">
          <router-view v-slot="{ Component }">
            <component :is="Component" />
          </router-view>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onErrorCaptured, watch, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Lock, User, Location, Wallet, Van, TakeawayBox, ChatDotRound, Star, Ticket, Setting, Service } from '@element-plus/icons-vue'
import { getCookieValue } from '../utils/request'
import { useUserStore } from '../stores/modules/user'

const router = useRouter()
const userStore = useUserStore()
const currentMenu = ref('home')

// 用户信息
const userInfo = ref({
  id: userStore.userInfo?.id || '',
  nickName: getCookieValue('nickName') || '未登录用户',
  email: userStore.userInfo?.email || '未设置邮箱',
  icon: getCookieValue('icon') || '',
  telephone: userStore.userInfo?.telephone || '',
  orderCount: 0,
  favoriteCount: 0,
  couponCount: 0
})

// 添加错误边界处理
const handleError = (error) => {
  console.error('组件错误：', error)
  ElMessage.error('页面加载出现错误，请稍后重试')
  return false
}

// 添加数据加载和错误处理
const loadUserData = async () => {
  try {
    userInfo.value = {
      id: userStore.userInfo?.id || '',
      nickName: getCookieValue('nickName') || '未登录用户',
      email: userStore.userInfo?.email || '未设置邮箱',
      icon: getCookieValue('icon') || '',
      telephone: userStore.userInfo?.telephone || '',
      orderCount: userStore.userInfo?.orderCount || 0,
      favoriteCount: userStore.userInfo?.favoriteCount || 0,
      couponCount: userStore.userInfo?.couponCount || 0
    }
  } catch (error) {
    console.error('加载用户数据失败：', error)
    ElMessage.error('加载用户数据失败，请刷新页面重试')
  }
}

onMounted(() => {
  loadUserData()
})

// 页面跳转方法
const handleNavigation = (routeName) => {
  try {
    router.push({ name: routeName })
  } catch (error) {
    console.error('页面跳转失败：', error)
    ElMessage.error('页面跳转失败，请重试')
  }
}

// 定义菜单项
const menuItems = [
  { key: 'home', label: '用户主页', icon: 'User', routeName: 'UserHome' },
  { key: 'orders', label: '我的订单', icon: 'Van', routeName: 'UserOrders' },
  { key: 'after_sale', label: '我的售后', icon: 'Service', routeName: 'UserAfterSale' },
  { key: 'comments', label: '我的评论', icon: 'ChatDotRound', routeName: 'UserComments' },
  { key: 'favorites', label: '我的收藏', icon: 'Star', routeName: 'UserFavorites' },
  { key: 'coupons', label: '优惠券', icon: 'Ticket', routeName: 'UserCoupons' },
  { key: 'address', label: '收货地址', icon: 'Location', routeName: 'UserAddress' },
  { key: 'service', label: '平台客服', icon: 'Service', routeName: 'UserService' },
  { key: 'settings', label: '账户设置', icon: 'Setting', routeName: 'UserSettings' },
]
</script>

<style scoped>
:root {
  --primary-color: #ff4d4f;
  --primary-color-light: #fff2f0;
  --danger-color: #ff7875;
  --bg-color: #fff;
  --bg-color-page: #f5f5f5;
  --text-regular: #333;
  --text-secondary: #888;
}

.user-center {
  padding: 20px 0;
  background-color: var(--bg-color-page);
  min-height: calc(100vh - 200px);
}

.container {
  width: 1200px;
  margin: 0 auto;
}

.user-center-layout {
  display: flex;
  gap: 20px;
}

/* 侧边栏样式 */
.sidebar {
  width: 240px;
  background: var(--bg-color);
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

/* 个人信息区 */
.user-brief h3 {
  color: #ff4d4f;
  font-weight: bold;
  margin: 10px 0 4px 0;
  font-size: 20px;
}
.user-brief p {
  color: var(--text-secondary);
  margin: 0;
}

/* 菜单高亮 */
.menu-item.active {
  background: var(--primary-color);
  color: #fff;
}
.menu-item:hover {
  background: var(--primary-color-light);
  color: var(--primary-color);
}
.menu-item .el-icon {
  color: var(--primary-color);
}

/* 内容区样式 */
.content-area {
  flex: 1;
  background: var(--bg-color);
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 24px;
}

.user-card {
  margin-bottom: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.info-content {
  margin-left: 20px;
}

.info-content h2 {
  margin: 0 0 10px;
  font-size: 24px;
}

.info-content p {
  margin: 0;
  color: var(--text-secondary);
}

.user-stats {
  display: flex;
  justify-content: space-around;
  padding-top: 20px;
}

.stat-item {
  text-align: center;
  cursor: pointer;
}

.stat-item .number {
  display: block;
  font-size: 24px;
  color: var(--primary-color);
  font-weight: bold;
}

.stat-item .label {
  color: var(--text-regular);
}

/* 功能卡片样式 */
.feature-card {
  height: 100%;
  cursor: pointer;
  transition: all 0.3s;
}

.feature-card:hover {
  transform: translateY(-5px);
}

.feature-card h3 {
  margin: 0;
  font-size: 16px;
}

/* 订单类型样式 */
.order-types {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
}

.type-item {
  text-align: center;
}

.type-item .el-icon {
  font-size: 24px;
  margin-bottom: 5px;
  color: var(--primary-color);
}

/* 收藏预览样式 */
.preview-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.preview-list .el-image {
  width: 100%;
  height: 80px;
  border-radius: 4px;
}

/* 优惠券样式 */
.coupon-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.coupon-item {
  background: linear-gradient(to right, var(--primary-color), var(--danger-color));
  color: white;
  padding: 10px;
  border-radius: 4px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.coupon-item .amount {
  font-size: 20px;
  font-weight: bold;
}

/* 设置区域样式 */
.settings-section {
  margin-top: 20px;
}

.setting-card {
  text-align: center;
  padding: 20px;
  border-radius: 8px;
  background: var(--bg-color-light);
  cursor: pointer;
  transition: all 0.3s;
}

.setting-card:hover {
  background: var(--primary-color-light);
}

.setting-card .el-icon {
  font-size: 24px;
  margin-bottom: 10px;
  color: var(--primary-color);
}

.setting-card span {
  display: block;
  color: var(--text-regular);
}
.nav-menu {
  margin-top: 20px;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  margin-bottom: 8px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
  color: var(--text-regular);

  &:hover {
    background: var(--primary-color-light);
    color: var(--primary-color);
  }

  &.active {
    background: var(--primary-color);
    color: white;

    .el-icon {
      color: white;
    }
  }

  .el-icon {
    margin-right: 8px;
    font-size: 18px;
    color: var(--primary-color);
    transition: color 0.3s;
  }

  span {
    font-size: 14px;
  }
}

.user-brief {
  text-align: center;
  padding: 20px 0;
  border-bottom: 1px solid var(--el-border-color-lighter);
  margin-bottom: 20px;

  .el-avatar {
    margin-bottom: 10px;
  }

  h3 {
    color: var(--primary-color);
    font-weight: bold;
    margin: 10px 0 4px 0;
    font-size: 20px;
  }

  p {
    color: var(--text-secondary);
    margin: 0;
    font-size: 14px;
  }
}
</style>